<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<u-search placeholder="请输入商品名称" v-model="keyword" @custom="goodsSearch" @clear="clearGoodsSearch"></u-search>
		</view>
		
		<view class="u-text-center u-m-t-20 u-m-b-20">
			<u-tabs :list="sortList" bar-width="140" item-width="140" :current="sortCurrent" @change="change"></u-tabs>
		</view>
		
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(list,index) in categoryList" :key="list.id">
					<view v-for="(category,index) in list.children" :key="category.id" class="u-tab-item" :class="[current==category.id ? 'u-tab-item-active' : '']"
					 :data-current="category.id" @tap.stop="swichMenu(category.id)">
						<text class="u-line-1">{{category.name}}</text>
					</view>
				</view>
				
			</scroll-view>
			<scroll-view scroll-y class="right-box" @scrolltolower="loadMore">
				<view class="u-m-r-40 u-p-t-80" v-if="goodsList.length == 0">
					<u-empty text="暂无数据" mode="search"></u-empty>
				</view>
				
				<view class="page-view" v-else>
					<view class="class-item">
						<view class="item-container">
							<navigator class="thumb-box" v-for="(goods,index) in goodsList" :key="index" :url="'/pages/goods/goodsDetail?id=' + goods.id">
								<image class="item-menu-image" :src="goods.cover_url" mode=""></image>
								<view class="item-menu-name">{{goods.title}}</view>
							</navigator>
						</view>
					</view>
				</view>
				
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sortList: [
					{name: '默认'},
					{name: '销量'},
					{name: '推荐'},
					{name: '价格'},
					{name: '评论'}
				],
				sortCurrent: 0,
				categoryList: [],
				goodsList: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: '', // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				page: 1,
				keyword: "",
				isLastPage: false
			}
		},
		onLoad() {
			this.getData()
		},
		computed: {
			
		},
		methods: {
			async getData(){
				const params = {
					page: this.page,
					category_id: this.current,
					title: this.keyword
				}
				// 处理筛选数据请求
				if(this.sortCurrent == 1) params.sales = 1;
				if(this.sortCurrent == 2) params.recommend = 1;
				if(this.sortCurrent == 3) params.price = 1;
				if(this.sortCurrent == 4) params.comments_count = 1;
				
				const resp = await this.$u.api.goodsList(params);
				this.categoryList = resp.categories;
				this.goodsList = [...this.goodsList, ...resp.goods.data];
				this.isLastPage = resp.goods.next_page_url ? false : true;
			},
			change(index) {
				this.sortCurrent = index;
				this.initData();
			},
			/**
			 * 初始化数据
			 */
			initData(){
				this.page = 1;
				this.goodsList = [];
				this.getData();
			},
			
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(cid) {
				if(cid == this.current) return ;
				this.current = cid;
				this.initData()
				
			},
			async goodsSearch(){
				this.current = '';
				this.initData();
			},
			async clearGoodsSearch(){
				this.current = '';
				this.initData();
			},
			async loadMore(){
				if(this.isLastPage){
					this.$u.toast('到底了~')
					return
				}
				this.page = this.page + 1;
				this.getData()
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-type-primary;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		background-color: rgb(250, 250, 250);
	}
	.page-view {
		padding: 16rpx;
	}
		.class-item {
			margin-bottom: 30rpx;
			background-color: #fff;
			border-radius: 8rpx;
		}
		
		.item-menu-name {
			font-weight: normal;
			font-size: 24rpx;
			color: $u-main-color;
		}
		
		.item-container {
			display: flex;
			flex-wrap: wrap;
		}
		
		.thumb-box {
			width: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			margin: 20rpx 0;
		}
		
		.item-menu-image {
			width: 180rpx;
			height: 240rpx;
		}
	</style>
	